<div class="row">
	<div class="box col-md-12">
		<div class="box-inner">
			<div class="box-header well">
                <h2><i class="glyphicon glyphicon-info-sign"></i> Thông tin cơ bản</h2>

                <div class="box-icon">
                    <a href="#" class="btn btn-minimize btn-round btn-default"><i class="glyphicon glyphicon-chevron-up"></i></a>
                    <a href="#" class="btn btn-close btn-round btn-default"><i class="glyphicon glyphicon-remove"></i></a>
                </div>
            </div>
			<div class="box-content row form-horizontal">
				<div class="form-group">
					<label class="col-sm-3 col-md-3 col-lg-2 control-label">Người đặt:</label>
					<div class="col-sm-9 col-md-9 col-lg-10"><?php
						echo $cart->getUser()->getUsername();
					?></div>
				</div><div class="form-group">
					<label class="col-sm-3 col-md-3 col-lg-2 control-label">Quản lý:</label>
					<div class="col-sm-9 col-md-9 col-lg-10"><?php
						$mod = $cart->getMod();
						if(empty($mod))
							echo ' - ';
						else
							echo html_escape($mod->getUsername());
					?></div>
				</div><div class="form-group">
					<label class="col-sm-3 col-md-3 col-lg-2 control-label">Tên người nhận:</label>
					<div class="col-sm-9 col-md-9 col-lg-10"><?php
						echo html_escape($cart->getCusname());
					?></div>
				</div><div class="form-group">
					<label class="col-sm-3 col-md-3 col-lg-2 control-label">Số điện thoại:</label>
					<div class="col-sm-9 col-md-9 col-lg-10"><?php
						echo html_escape($cart->getCusphone());
					?></div>
				</div><div class="form-group">
					<label class="col-sm-3 col-md-3 col-lg-2 control-label">Email:</label>
					<div class="col-sm-9 col-md-9 col-lg-10"><?php
						echo html_escape($cart->getCusemail());
					?></div>
				</div><div class="form-group">
					<label class="col-sm-3 col-md-3 col-lg-2 control-label">Nội dung:</label>
					<div class="col-sm-9 col-md-9 col-lg-10"><?php
						echo html_escape($cart->getDetail());
					?></div>
				</div><div class="form-group">
					<label class="col-sm-3 col-md-3 col-lg-2 control-label">Ngày đặt:</label>
					<div class="col-sm-9 col-md-9 col-lg-10"><?php
						echo date('d - m - Y H:i', $cart->getDaterequest());
					?></div>
				</div><div class="form-group">
					<label class="col-sm-3 col-md-3 col-lg-2 control-label">Ngày giao:</label>
					<div class="col-sm-9 col-md-9 col-lg-10"><?php
						$deliver = $cart->getDatedelivery();
						if(empty($deliver))
							echo null;
						else
							echo date('d - m - Y H:i', $deliver);
					?></div>
				</div>
			</div>
		</div>
	</div>
</div>
<div class="row">
	<div class="box col-xs-12">
		<div class="box-inner">
			<div class="box-header well">
                <h2><i class="glyphicon glyphicon-info-sign"></i> Đơn hàng chi tiết</h2>

                <div class="box-icon">
                    <a href="#" class="btn btn-minimize btn-round btn-default"><i class="glyphicon glyphicon-chevron-up"></i></a>
                    <a href="#" class="btn btn-close btn-round btn-default"><i class="glyphicon glyphicon-remove"></i></a>
                </div>
            </div>
			<div class="box-content"><?php
			echo form_open(null,array(
				'class'=>'form-horizontal','role'=>'form'
			));
				?><table class="table table-striped table-bordered responsive" id="detail_table">
					<thead>
						<tr>
							<th>Mã sản phẩm</th>
							<th>Tên sản phẩm</th>
							<th>Màu sắc</th>
							<th>Kích cỡ</th>
							<th>Số lượng</th>
							<th>Giá</th>
							<th></th>
						</tr>
					</thead>
					<tbody><?php
			if(!empty($cart_detail)):
				foreach($cart_detail as $v):
				
					$color = $v->getColor();
					$color_id = 0;
					if(!empty($color)):
						$color_id = $v->getColor()->getId();
					endif;
					$input_info = 'id='.$v->getId().';color='.$color_id;
				?>
					<tr class="detail_row" id="product_<?php echo $v->getProduct()->getId(); ?>">
						<input type="hidden" class="product_hidden_info" name="product[]" value="<?php echo $input_info; ?>" />
						<td><?php echo $v->getProduct()->getId(); ?></td>
						<td><?php echo html_escape($v->getProduct()->getName()); ?></td>
						<td><?php  ?></td>
						<td><?php  ?></td>
						<td class="text-right quantity_td">
							<input name="product_<?php echo $v->getId();?>" value="<?php echo $v->getQuantity(); ?>" class="form-control text-right product_quantity"/></td>
						<td class="price_td"><?php echo $v->getPrice(); ?></td>
						<td></td>
					</tr>
				<?php
				endforeach;
				?></tbody><tfoot><tr>
					<td colspan="5" class="text-right"><b>Tổng giá:</b></td>
					<td colspan="2" class="text-left"><b id="total_price"><?php echo $cart->getTotalmoney(); ?></b></td>
				</tr></tfoot><?php
			else:
				?><tr id="empty_cart">
					<td colspan="7">
						Không có sản phẩm nào trong đơn hàng này
					</td>
				</tr></tbody><?php
			endif;
				?>
			</table>
				<div class="form-group">
					<label class="col-sm-3 col-md-3 col-lg-2 control-label">Tình trạng hóa đơn:</label>
					<div class="col-sm-3 col-md-3 col-lg-3"><?php
						$c_arr = array();
						if(!empty($cart_status)):
							foreach($cart_status as $c_v)
								$c_arr[$c_v->getName()] = html_escape($c_v->getVal());
						endif;
						
						echo form_dropdown('status', $c_arr,
							set_value('status', $cart->getStatus()),'class="form-control"');
						echo form_error('status');
					?></div>
				</div>
				<div class="form-group">
					<div class="col-sm-offset-5 col-md-offset-4 col-lg-offset-3 col-sm-7 col-md-8 col-lg-9"><?php
						echo form_submit(array(
							'name'=>'submit','value'=>'Sửa hóa đơn',
							'class'=>'btn btn-success'
						)).' '.form_reset(array(
							'name'=>'reset','value'=>'Nhập lại',
							'class'=>'btn btn-warning'
						)).' ';
					if($cart->getStatus() == 0):
						?><button type="button" class="btn btn-info" data-toggle="modal" data-target="#product_add">Thêm sản phẩm</button><?php						
					endif;
					?></div>
				</div>
				<?php
			echo form_close();
			?></div>
		</div>
	</div>
</div>
<div class="modal fade" id="product_add" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
  <div class="modal-dialog">
	<div class="modal-content">
	  <div class="modal-header">
		<button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">&times;</span><span class="sr-only">Close</span></button>
		<h4 class="modal-title" id="myModalLabel">Thêm sản phẩm</h4>
	  </div>
	  <div class="modal-body">
		<input type="text" name="product_name" value="" class="form-control" id="search_input">
		<button type="button" class="btn btn-info" id="search">Tìm</button>
		<table class="table">
			<thead>
				<tr>
					<th>Mã</th>
					<th>Tên</th>
					<th>Đơn giá</th>
					<th>Màu sắc</th>
					<th>Kích cỡ</th>
				</tr>
			</thead><tbody id="term_table_product">
			
			</tbody>
		</table>
	  </div>
	  <div class="modal-footer">
		<button type="button" class="btn btn-default" data-dismiss="modal">Đóng</button>
		<button type="button" class="btn btn-primary" id="term_product_add">Lưu</button>
	  </div>
	</div>
  </div>
</div>
<script>
ajax = false;
$('#search').click(function(){
	if(ajax){
	  ajax.abort();
	  ajax = 0;
	}
	data = {value:$('#search_input').val()};
	ajax = $.ajax({
		url: '/admin/carts/search/',
		data: data,
		dataType: 'json',
		type: 'POST'
	}).done(function(data, e){
		$('#term_table_product').html('');
		if(data.length == 0){
			return false;
		}
		$.each(data, function(k,v){
			
			var e_c = '';
			$.each(v.color, function(k_c, v_c){
				console.log(v_c);
				e_c = e_c + '<span data-eid="'+v_c.id+'" title="'
						+v_c.name+'" class="term_product_color" style="padding:5px 8px;background:#'
						+v_c.val+'"></span>';

			});
			var e = $('<tr data-pid="'+v.id+'"><td>'
					+v.id
					+'</td><td class="term_product_name">'+v.name+'</td><td class="term_product_price">'+v.price+'</td><td class="color">'+e_c+'<td><td class="size"></td></tr>');

					
			$(e).find('.color').html(e_c);
			
			$('#term_table_product').append(e);
		});
	}).fail(function(){
		alert('Lỗi, thử lại');
	}).error(function(){
		alert('Lỗi');
	});
});
$(document).on('click','#term_table_product tr', function(){
	$(this).toggleClass('active');
});
$(document).on('click','#term_table_product tr .term_product_color', function(e){
	e.stopImmediatePropagation();
})
$('#term_product_add').click(function(){
	var new_p = $('#term_table_product').find('tr.active');
	var str = null;
	$.each(new_p, function(k, v){
		
		//tim san pham ton tai hay chua
		var id = $(v).data('pid');
		var name =  $(v).find('.term_product_name').text();
		var color = $(v).find('.term_product_color').data('eid');
		var price = $(v).find('.term_product_price').text();
		var p = $('#product_'+id);
		
		if(p.length != 0){
			var p_quantity = p.find('.product_quantity');
			p_quantity.val(parseInt(p_quantity.val())+1);
			p.find('input.product_hidden_info').val('id='+id+';color='+color);
		} else {
			console.log('asd');
			str += '<tr class="detail_row" id="product_'+id+'"><input type="hidden" class="product_hidden_info" name="product[]" value="id='
				+id+';color='+color+'" /><td>'+id+'</td><td>'+name+'</td><td></td><td></td><td><input class="form-control text-right product_quantity" value="1" name="product_'
				+id+'" /></td><td>'
				+price+'</td></tr>';
			$('#detail_table tbody').append(str);	
		}
		
	});
});
</script>